package com.lianxi.Courses.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.ResponseCodeConstant;
import com.lianxi.Courses.entity.Comment;
import com.lianxi.Courses.entity.Resource;
import com.lianxi.Courses.entity.User;
import com.lianxi.Courses.exception.FileDownloadErrorException;
import com.lianxi.Courses.exception.RequestParameterErrorException;
import com.lianxi.Courses.service.CommentService;
import com.lianxi.Courses.service.ResourceService;
import com.lianxi.Courses.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

/**
 * 翔哥
 * 2025年10月13日
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private CommentService commentService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(Resource resource, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Resource> resourcePage = resourceService.findPage(resource,pageNo, pageSize);
        mav.addObject("resourcePage", resourcePage);
        mav.setViewName("backend/back_resourceSet");

        mav.addObject("resource",resource);
        return mav;
    }

    @RequestMapping("/updateStatus")
    public ResponseResult updateStatus(String id ) {
        ResponseResult result =new ResponseResult();
        try {
            resourceService.updateStatus(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/findById")
    public ModelAndView findById(String id) {
        ModelAndView mav = new ModelAndView();
        try {
            Resource resource =resourceService.findById(id);
            mav.addObject("resource",resource);
            mav.setViewName("backend/back_resourceDetailSet");

        } catch (RequestParameterErrorException e) {
            mav.addObject("errorMessage", e.getMessage());
            mav.setViewName("backend/back_login");
        } catch (Exception e) {
            mav.addObject("UserMsg", "服务器内部异常");
            mav.setViewName("backend/back_login");
        }
        return mav;
    }


    @RequestMapping("/Download")
    public void  Download(String id, HttpServletResponse response, HttpSession session) {

        System.out.println("进来了**************"+id);

        try {
            resourceService.downloadFile(id, response, session);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (RequestParameterErrorException e) {
            throw new RuntimeException(e);
        } catch (FileDownloadErrorException e) {
            throw new RuntimeException(e);
        }

    }



}
